import { UIBase, UIClass } from "../Common/UIBase";
import { MathUtils } from "../Utils/MathUtils";


interface IUIScript extends UIBase { }

class UIManager {
    private static instance: UIManager = null;
    public static get inst(): UIManager {
        if (this.instance === null) {
            this.instance = new UIManager();
        }
        return this.instance;
    }

    // ____zorderNodes____
    private rootUI: cc.Node = null; // UI层
    private rootTips: cc.Node = null; // tips层
    private rootAlert: cc.Node = null; // 系统层

    private uiStacks: UIBase[] = []; // UI堆栈

    private isLoadingUI: boolean = false; // 是否正在加载UI

    private waitingLoadingUIParams: { class: any, callback?: any }[] = [];

    private initRenderZorderNodes(): void {
        const canvasNode: cc.Node = cc.find("Canvas");
        const rootContainers: string[] = ["rootUI", "rootTips", "rootAlert"];
        const rootIndexs: number[] = [1, 2, 3];

        for (let index = 0; index < rootContainers.length; index++) {
            const name: string = rootContainers[index];
            if (!cc.isValid(this[name])) {
                const node: cc.Node = new cc.Node();
                canvasNode.addChild(node, rootIndexs[index]);
            }
        }

    }
    public pushUI<T extends UIBase>(uiclass: UIClass<T>, compeleteCallBack?: (uiScript: T) => void): void {
        this.initRenderZorderNodes();
        // 判断是否正在加载ui  如果正在加载 则等加载完再创建第二个ui 保证时序
        if (this.isLoadingUI) {
            this.waitingLoadingUIParams.push({ class: uiclass, callback: compeleteCallBack });
            return;
        }
        this.isLoadingUI = true; // 正在加载UI
        cc.loader.loadRes(uiclass.getPrefabPath(), cc.Prefab, (err: Error, res: cc.Prefab) => {
            const uiNode: cc.Node = cc.instantiate(res);
            const uiScript: T = uiNode.getComponent(uiclass);
            this.uiStacks.push(uiScript);
            this.rootUI.addChild(uiNode);

            this.isLoadingUI = false;
            if (compeleteCallBack) {
                compeleteCallBack(uiScript);
            }
            this.updateUIStacksRender();
            if (this.waitingLoadingUIParams.length > 0) {
                const uiParam = this.waitingLoadingUIParams.shift();
                this.pushUI(uiParam.class, uiParam.callback);
            }

        });
    }

    public popUI(uiScript: IUIScript): void {
        MathUtils.deleteCellInArray(this.uiStacks, (ui: IUIScript) => {
            if (ui === uiScript) {
                ui.node.destroy();
                return true;
            }
            else {
                return false;
            }
        })
        this.updateUIStacksRender();

    }
    // 刷新UI堆栈渲染队列
    private updateUIStacksRender(): void {

        let funScreenIndex: number = -1; // 全屏界面的索引
        for (let index = this.uiStacks.length - 1; index >= 0; index--) {
            const ui: IUIScript = this.uiStacks[index];
            if (ui.isFullScreen && funScreenIndex === -1) {
                funScreenIndex = index;
            }
            ui.node.active = funScreenIndex === -1 || funScreenIndex === index;
        }
    }
}

const UIMgr = UIManager.inst;
export default UIMgr;   